﻿using MediatR;
using W.Purchase.API.Application.Command.SaleCommand;
using W.Purchase.Repository.Impl;
using W.Purchase.Repository.Interface;

namespace W.Purchase.API.Application.CommandHandler.SaleCommandHandler
{
    public class SaleOrderDeleteCommandHandler : IRequestHandler<SaleOrderDeleteCommand, int>
    {
        private readonly ISalesOrdersRepository salesOrdersRepository;

        public SaleOrderDeleteCommandHandler(ISalesOrdersRepository salesOrdersRepository)
        {
            this.salesOrdersRepository = salesOrdersRepository;
        }

        public async Task<int> Handle(SaleOrderDeleteCommand request, CancellationToken cancellationToken)
        {
            var list = await salesOrdersRepository.GetAsync(w => w.SalesOrdersId == request.SalesOrdersId);
            list.FirstOrDefault().IsDelete = true;
            list.FirstOrDefault().DeleteTime = DateTime.Now;
            return await salesOrdersRepository.Update(list.FirstOrDefault());
        }
    }
}
